import {Base64} from 'js-base64'

const prefix = 'ficiton_reader_';
const StorageGetter = (key) => {
    return localStorage.getItem(prefix + key);
}
const StorageSetter = (key, val) => {
    return localStorage.setItem(prefix + key, val);
}
//数据解密
const getBSONP = (url, callback) => {
    // http://html.read.duokan.com/mfsv2/secure/s010/60009/file?nonce=87e8e80bd9a84314badbd9230ff521b4&token=89GiFGpK01J7WSSnxHnjoefpgNPv-zrNCurl0z1EkRx4OZm4-aB36_TllcymXfewETa58Q9VLD9jJcC4MS7oa0uRTgC6JG9Poed648pU41U&sig=FxmsSqJuj3BM0pQ07XKq13UGJLY

    return jsonp({
        url: url,
        callbackFunc: 'duokan_fiction_chapter',
        callback: function (result) {
            const data = Base64.decode(result);//解密
            // console.log(data);
            callback(data);
        }
    })
};

//jsonp请求
function jsonp(setting) {
    setting.data = setting.data || {}
    setting.callbackKey = setting.callbackKey || 'callback'
    setting.callbackFunc = setting.callbackFunc || function () { }
    setting.data[setting.callbackKey] = setting.callbackFunc  //callback="xxxxxx"
    window[setting.callbackFunc] = function (data) {
        setting.callback(data);
    }

    var script = document.createElement('script')
    script.src = setting.url
    document.head.appendChild(script)
    document.head.removeChild(script)
}


const util = {
    StorageGetter, StorageSetter, getBSONP
}
export default util